package com.shanzmoo.admin.web.trade;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.google.common.collect.Maps;
import com.shanzmoo.base.exception.PayException;
import com.shanzmoo.base.util.SResponseUtils;
import com.shanzmoo.core.pay.sdk.alipay.config.Configs;
import com.shanzmoo.db.trade.service.IPayOrderCallbackService;
import com.shanzmoo.core.service.ITradeService;
import com.shanzmoo.core.util.SWxPayUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.Map;

/**
 * 支付回调 管理
 * @author Gzhao 2021/1/3
 */
@Slf4j
@RestController
@RequestMapping("open/callback")
@Api(tags = "支付回调")
public class OpenCallbackController {

    @Autowired
    private ITradeService tradeService;

    @Autowired
    private IPayOrderCallbackService callbackService;


    @GetMapping("test")
    public String test(){
        return "200";
    }

    @ApiOperation("支付宝回调")
    @PostMapping("alipay")
    public void success(HttpServletRequest request, HttpServletResponse response) {
        Map<String, String> map = Maps.newHashMap();
        Enumeration<String> parameterNames = request.getParameterNames();

        while (parameterNames.hasMoreElements()) {
            String parameter = parameterNames.nextElement();
            log.info("parametername:{}, value:{}", parameter, request.getParameter(parameter));
            if (!parameter.toLowerCase().equals("sign_type")) {
                map.put(parameter, request.getParameter(parameter));
            }
        }

        log.info("支付宝回调信息：{}", JSON.toJSONString(map));

        callbackService.saveCallbackInfo(map.get("trade_no"), false, JSON.toJSONString(map));
        try {
            boolean result = AlipaySignature.rsaCheckV2(map, Configs.getAlipayPublicKey(), "utf-8", Configs.getSignType());
            if (result) {
                tradeService.doAliPayCallback(map);

                SResponseUtils.outputStr(response, "success");
            } else {
                SResponseUtils.outputStr(response, "unSuccess");
            }
        } catch (AlipayApiException e) {
            throw new PayException("支付回调失败: " + e.getMessage());
        }
    }

    @ApiOperation("微信支付回调")
    @PostMapping("wxpay")
    public void wxNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String notifyData = SWxPayUtils.obtainNotifyData(request);

        // 回调返回微信信息
        String resXml = tradeService.doWxNotify(notifyData);

        log.info("通知微信服务器的报文：{}", resXml);
        SResponseUtils.outputStr(response, resXml);
    }
}
